<?php

namespace App\Http\Requests\Api;


class RoleRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
            case 'PATCH':
                $role = $this->route('role');
                $id = $role->id;
                return [
                    'name' => 'required|min:2|unique:roles,name,' . $id,
                    'permissions' => 'required|array',
                ];
                break;
            case 'POST':
                return [
                    'name' => 'required|min:2|unique:roles',
                    'permissions' => 'required|array',
                ];
                break;
        }
    }

    public function messages()
    {
        return [
            'name.required' => '角色名不能为空',
            'name.unique' => '角色名重复',
            'name.min' => '角色名最少需要2个字符',
            'permissions.required' => '功能权限不能为空',
            'permissions.array' => '功能权限必须提交数组',
        ];
    }
}
